﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace ZEFIT
{
    class MYSQLAdabterRapport : Adabter
    {
        public override List<object> Select()
        {
            String conString = "SELECT ra.ID, ra.Von, ra.Bis, bt.ID, bt.Benutzername, ag.ID, ag.Name, pj.ID, pj.Name" +
                " FROM tbl_rapport as ra, tbl_benutzer as bt, tbl_aufgabe as ag, tbl_projekt as pj" +
                " WHERE ra.Benutzer_ID = bt.ID AND ra.Aufgabe_ID = ag.ID AND ra.Projekt_ID = pj.ID;";
            List<object> reader = MYSQLConnecter.SelectRows(new MySqlCommand(conString));
            //List<Rapport> rapportList = new List<Rapport>();
            //foreach (List<object> tempList in controller.Select())
            //{
            //    Rapport temp = new Rapport();
            //    temp.Id = (int)tempList[0];
            //    temp.WorkedFrom = (DateTime)tempList[1];
            //    temp.WorkedTo = (DateTime)tempList[2];
            //    User temp2 = new User();
            //    temp2.Id = (int)tempList[3];
            //    temp2.UserName = (String)tempList[4];
            //    temp.User = temp2;
            //    WorkingCategory temp3 = new WorkingCategory();
            //    temp3.Id = (int)tempList[5];
            //    temp3.Name = (String)tempList[6];
            //    temp.Category = temp3;
            //    Project temp4 = new Project();
            //    temp4.Id = (int)tempList[7];
            //    temp4.Name = (String)tempList[8];
            //    temp.Project = temp4;
            //    rapportList.Add(temp);
            //}
            return reader;
        }

        public override void Insert(ref Data data)
        {
            Rapport rapport = (Rapport)data;

            String conString = "INSERT INTO tbl_rapport (Von, Bis, Benutzer_ID, Aufgabe_ID, Projekt_ID) VALUES" +
                " ('" + rapport.WorkedFrom.ToString("yyyy-MM-dd HH:mm:ss") + "', '" + rapport.WorkedTo.ToString("yyyy-MM-dd HH:mm:ss") + "', " + rapport.User.Id + ", " + rapport.Category.Id + ", " + rapport.Project.Id + ");";
            rapport.Id = MYSQLConnecter.SendInsert(new MySqlCommand(conString));

        }

        public override void Update(Data data)
        {
            Rapport rapport = (Rapport)data;

            String conString = "UPDATE tbl_rapport" +
                " SET Von = '" + rapport.WorkedFrom.ToString("yyyy-MM-dd HH:mm:ss") + "', Bis = '" + rapport.WorkedTo.ToString("yyyy-MM-dd HH:mm:ss") + "', Benutzer_ID = " + rapport.User.Id + ", Aufgabe_ID = " + rapport.Category.Id + ", Projekt_ID = " + rapport.Project.Id +
                " WHERE ID = " + rapport.Id + ";";
            MYSQLConnecter.Send(new MySqlCommand(conString));
        }

        public override void Delete(int id)
        {
            String conString = "DELETE FROM tbl_rapport" +
                " WHERE ID = " + id + ";";
            MYSQLConnecter.Send(new MySqlCommand(conString));
        }
    }
}
